package com.shanghaionstar.user.service;

import android.content.Context;
import android.os.Bundle;
import android.widget.EditText;

import com.onstar.cn.api.UsersService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.more.request.PrepaidCardRequest;
import com.onstar.cn.more.response.PrepaidCardResponse;
import com.shanghaionstar.enumtype.OSTypeEnum;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Mengzhou Gu on 2016/8/3.
 */
public class UsePrepayCardService extends BaseService<PrepaidCardResponse> {
    private Context context;
    private EditText et_carnumber_value;
    private EditText et_password_value;
    private Bundle bundle;

    public UsePrepayCardService(Context context,EditText et_carnumber_value,EditText et_password_value,Bundle bundle){
        this.context=context;
        this.et_carnumber_value=et_carnumber_value;
        this.et_password_value=et_password_value;
        this.bundle=bundle;
    }
    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public PrepaidCardResponse jsonToBean(String json) {
        return GsonHelper.fromJson(json,PrepaidCardResponse.class);
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public Observable<PrepaidCardResponse> getObserval(final String subscriberId, final String accountId){
        return Observable.defer(new Func0<Observable<PrepaidCardResponse>>() {
            @Override
            public Observable<PrepaidCardResponse> call() {
                try {
                UsersService service = new UsersService(Constants.CURRENT_SERVER_URL);
                //		UsersService service=new UsersService("http://10.16.138.155:8080");
                RequestEntity entity = NetConnectUtil.getAuthRequestEntity(context);
                    PrepaidCardRequest request = new PrepaidCardRequest();
                request.setCardNo(et_carnumber_value.getText().toString().trim());
                request.setPassWord(et_password_value.getText().toString().trim());
                request.setOsType(String.valueOf(OSTypeEnum.ANDROID_SMALL));
                request.setOperation("VALIDATE");
                entity.setBody(GsonHelper.toJson(request));
                entity = service.activatePrepaidCard(entity, subscriberId,
                        accountId, bundle.getString("vin"));
                    entity =  HttpInjectUtils.injectHttp(UsePrepayCardService.this.getClass(), "getObserval", entity,String.class,String.class);
                    return Observable.just(GsonHelper.fromJson(BusinessUtil.getResponse(getDatas(entity), context),PrepaidCardResponse.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }
}
